package cn.gov.pbc.gd.credit.user.service.realuser.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.liu.jbase.ResultVo;
import com.liu.jbase.service.impl.BaseServiceImpl;
import com.liu.tools.LogUtil;
import com.liu.tools.RandomUtil;

import cn.gov.pbc.gd.credit.api.realuser.IRealUserApi;
import cn.gov.pbc.gd.credit.center.service.user.ICCUserService;
import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.util.PropUtil;
import cn.gov.pbc.gd.credit.user.service.realuser.RealUserTaskService;
import cn.gov.pbc.gd.credit.vo.center.ReqCCUserVo;
import cn.gov.pbc.gd.credit.vo.center.ResCCUserVo;
import cn.gov.pbc.gd.credit.vo.realuser.RealUserVo;
import cn.gov.pbc.gd.credit.vo.realuser.ResRealUserVo;

@Service
public class RealUserTaskServiceImpl extends BaseServiceImpl<ResRealUserVo, RealUserVo, IRealUserApi> implements RealUserTaskService{
	
	private static Logger logger = LoggerFactory.getLogger(RealUserTaskServiceImpl.class);
	
	private static Properties prop = PropUtil.loadProperties("prop.properties");
	
	@Autowired
	private ICCUserService ccUserService;
	
	@Autowired
	private IRealUserApi realUserApi;
	/**
	 * 定时修改真实用户的密码
	 * @throws ParseException 
	 */
	public void modifyPwdInFixedTime() throws ParseException {
		HttpServletRequest request = null ;
		// 1.获取多少时间修改一次真实用户的密码
		String modifyPwdTime = ((String) prop.get("modifyPwdTime")).trim();
		// 2.获取所有真实用户的信息
		RealUserVo realUser = new RealUserVo();
		ResultVo<List<ResRealUserVo>> realUserResult = super.list(realUser, request);
		// 3.循环每一个真实用户，将当前时间与每个真实用户的密码修改时间做差值，如果差值>配置的修改时间的值，那么执行修改密码的操作，否则不执行
		List<ResRealUserVo> realUserList = realUserResult.getData();
		for (int f = 0; f < realUserList.size(); f++) {
			Date pwdUpdate = realUserList.get(f).getUpdatePwdTime();
			Date nowDate = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			long value = (sdf.parse(sdf.format(nowDate)).getTime() - sdf.parse(sdf.format(pwdUpdate)).getTime()) / 1000;// 当前时间和真实用户密码修改时间的差值(秒)
			String password = RandomUtil.randomStr(11);
			if (value > Long.valueOf(modifyPwdTime)) {
				/**
				 * 需要先登录征信中心，如果登录成功那么执行修改征信中心密码操作并修改真实用户的密码，如果登陆失败，不修改密码
				 */
				ReqCCUserVo loginVo = new ReqCCUserVo();
				loginVo.setPassword(realUserList.get(f).getUserPwd());
				loginVo.setUserid(realUserList.get(f).getAccount());
				ResultVo<ResCCUserVo> resultLogin = ccUserService.login(loginVo, request);// 登陆人行征信中心
				if (CodeConstant.SUCCESS.equals(resultLogin.getCode())) {
					ReqCCUserVo ccUserVo = new ReqCCUserVo();
					ccUserVo.setOldpassword(realUserList.get(f).getUserPwd());
					ccUserVo.setNewpasswordvalid(password);
					ccUserVo.setPassword(password);
					ccUserVo.setCookies(resultLogin.getData().getCookies());
					ccUserVo.setUserid(realUserList.get(f).getAccount());
					ResultVo<ResCCUserVo> resCCUserVo = ccUserService.modifyPwd(ccUserVo, request);// 修改征信中心的密码
					if (!CodeConstant.SUCCESS.equals(resCCUserVo.getCode())) {
						logger.info(LogUtil.msg("UserServiceImpl:modifyPwdInFixedTime",
								"定时修改征信中心密码失败, modifyResult=" + JSON.toJSONString(resCCUserVo)));
						return;
					}

					logger.info("真实用户" + realUserList.get(f).getUserName() + "定时修改征信中心密码成功，开始修改真实用户密码");

					RealUserVo realUserVo = new RealUserVo();
					realUserVo.setId(realUserList.get(f).getId());
					realUserVo.setUserPwd(password);
					int flag = realUserApi.update(realUserVo);// 修改柜台前置系统的真实用户的密码
					if (flag == 0) {
						logger.info("真实用户" + realUserList.get(f).getUserName() + "定时修改征信中心密码成功，修改真实用户密码失败");
						return;
					} else {
						loginVo.setPassword(password);
						resultLogin = ccUserService.login(loginVo, request);// 重新登陆人行征信中心
						if (!CodeConstant.SUCCESS.equals(resultLogin.getCode())) {
							logger.info(
									"真实用户" + realUserList.get(f).getUserName() + "定时修改征信中心密码成功，修改真实用户密码也成功，重新登陆征信中心失败");
							return;
						} else {
							logger.info("真实用户" + realUserList.get(f).getUserName()
									+ "定时修改征信中心密码成功，修改真实用户密码也成功，重新登陆征信中心也成功");
							return;
						}

					}
				} else {
					logger.info("真实用户" + realUserList.get(f).getUserName() + "登陆征信中心失败");
					return;
				}
			}
		}
	}

}
